﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using UE.SharePoint.ImportExport.BLL; 
using System.Web.UI;
using System.Text;
using Microsoft.SharePoint.Utilities;
using System.Globalization;
using UE.SharePoint.ImportExport.Resources;
using System.IO;
using System.Web.UI.WebControls; 
using System.Web;

namespace UE.SharePoint.ImportExport.Layouts
{
    public partial class ExportSchema : LayoutsPageBase
    {
        private string listId = string.Empty;
        private string isDlg = string.Empty;

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            litDialogTitle.Text = ImportExportResource.ExportSchemaPage_Title;
            litDialogDescription.Text = ImportExportResource.ExportSchemaPage_Description;
            litFormatLabel.Text = ImportExportResource.ExportSchemaPage_FormatLabel;
            litFileNameLabel.Text = ImportExportResource.ExportSchemaPage_NameLabel;
            chkContainAdditionalColumns.Text = ImportExportResource.ExportSchemaPage_ContainAdditionalColumnsLabel;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Button btnOkButton = ((DialogMaster)this.Page.Master).OkButton;
            btnOkButton.Click += new EventHandler(btnOkButton_Click);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            listId = Request.QueryString["listId"];
            if (!Util.ValidateSPlistExist(SPContext.Current.Web, listId))
            {
                Response.Write("ListId 不正确！");
                Response.End();
            }

            listId = listId.Replace("{", "").Replace("}", "");
            isDlg = Request.QueryString["isDlg"];
             
            //ListItem listItem = new ListItem(".xlsx (Excel 2007/2010/2013)", "1");
            //listItem.Selected = true;
            //this.FormatList.Items.Add(listItem);
            this.FormatList.Items.Add(new ListItem(".xls (Excel 2003)", "2")); 
        }

        void btnOkButton_Click(object sender, EventArgs e)
        {
            string fileName = FileNameTextBox.Text;
            string fileExt = GetFileExt();
            string fullName = fileName + "." + fileExt;
            if (string.IsNullOrEmpty(fileName))
            {
                litFileNameError.Text = "You must input a file name!";
                return;
            }

            ExportManager exportManager = new ExportManager();
            IStatus status = new WorkStatus();
            using (MemoryStream stream = exportManager.ExportSchema(SPContext.Current.Site, SPContext.Current.Web, listId, fileName,chkContainAdditionalColumns.Checked,status))
            { 
                stream.Flush();
                stream.Position = 0;
                Response.AddHeader("Content-Disposition", ("attachment;filename=" + HttpUtility.UrlEncode(fullName,Encoding.UTF8).ToString()));
                Response.Charset = "UTF-8";
                Response.ContentType = "application/json";
                Response.BinaryWrite(stream.ToArray());
                stream.Close();
                stream.Dispose();
            }

            Response.End();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        string GetFileExt()
        {
            string ext = string.Empty;

            switch(FormatList.SelectedValue)
            {
                case "1":
                    ext = "xlsx";
                    break;
                case "2":
                    ext = "xls";
                    break; 
            }

            return ext;
        }
    }
}
